package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.outStorage.SaleOutStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageSaleListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageSaleMaterielListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStorageSaleListRequest;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStorageSaleMaterielListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;

import java.util.List;

public class OutStorageSaleListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView saleOutStorageList;

    private List<OutStorageSaleListBean.BodyBean> allData;

    private List<OutStorageSaleMaterielListBean.BodyBean> allMaterielData;

    private SaleOutStorageListAdapter adapter;

    private String mCondition;

    private int mFlag;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_sale_list);
        initView(OutStorageSaleListActivity.this);
        initData(OutStorageSaleListActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        saleOutStorageList = (PullableListView) activity.findViewById(R.id.saleOutStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.saleOutStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_out_storage_sale_list_title);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_out_storage_sale_outbound, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        ActivityUtils.initRightFunction(activity, ActivityUtils.getRightViewWithName(activity, "获取"), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                //列表页右上角获取按钮操作
                switch (mFlag) {
                    case OutStorageUtils.TAB_MATERIEL:
                        OutStorageSaleMaterielListRequest.request(activity, null, true, new GsonBeanListener<OutStorageSaleMaterielListBean>() {
                            @Override
                            public void onSuccess(OutStorageSaleMaterielListBean bean) {
                                mCondition = "";
                                allMaterielData = bean.getBody();
                                if (allMaterielData.isEmpty()) {
                                    Utils.middleToast(activity, getResources().getString(R.string.activity_out_storage_sale_list_no_data));
                                }
                                addOutStorageSaleListData(activity, null, ActivityUtils.getShowData(allMaterielData, 0), saleOutStorageList, true);
                            }
                        });
                        break;
                    case OutStorageUtils.TAB_SALE_OUT_STORAGE:
                        OutStorageSaleListRequest.request(activity, null, true, new GsonBeanListener<OutStorageSaleListBean>() {
                            @Override
                            public void onSuccess(OutStorageSaleListBean bean) {
                                mCondition = "";
                                allData = bean.getBody();
                                if (allData.isEmpty()) {
                                    Utils.middleToast(activity, getResources().getString(R.string.activity_out_storage_sale_list_no_data));
                                }
                                addOutStorageSaleListData(activity, ActivityUtils.getShowData(allData, 0), null, saleOutStorageList, true);
                            }
                        });
                        break;
                }
            }
        });

        saleOutStorageList.setPullUpFlag(false);
        saleOutStorageList.setPullDownFlag(false);
        mCondition = "";
        mFlag = OutStorageUtils.TAB_MATERIEL;
        getMaterielData(activity, mCondition);
//        resetData(activity);
//        setmScanListener((String scanResult, int length) -> {
//            mSaleNumber = QRCodeUtils.DecryptQRCode(scanResult);
//            getData(activity, mSaleNumber);
//        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_PURCHASE_ORDER, QRCodeConstant.TYPE_POSITION);
    }

    private void addOutStorageSaleListData(Context context, List<OutStorageSaleListBean.BodyBean> saleOutStorageList, List<OutStorageSaleMaterielListBean.BodyBean> saleOutStorageMaterielList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new SaleOutStorageListAdapter(context, saleOutStorageList, saleOutStorageMaterielList, mCondition, new OutStorageSaleListener() {
                @Override
                public void onNoDoubleClickListener(String condition) {
                    mCondition = condition;
                    switch (mFlag) {
                        case OutStorageUtils.TAB_MATERIEL:
                            getMaterielData(context, mCondition);
                            break;

                        case OutStorageUtils.TAB_SALE_OUT_STORAGE:
                            getSaleOutStorageData(context, mCondition);
                            break;
                    }
                }

                @Override
                public void onChangeTab(int flag) {
                    if (flag != mFlag) {
                        mFlag = flag;
                        mCondition = "";
                        switch (flag) {
                            case OutStorageUtils.TAB_MATERIEL:
                                getMaterielData(context, mCondition);
                                break;

                            case OutStorageUtils.TAB_SALE_OUT_STORAGE:
                                getSaleOutStorageData(context, mCondition);
                                break;
                        }
                    }
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private void getSaleOutStorageData(Context context, String condition) {
        OutStorageSaleListRequest.request(context, condition, false, new GsonBeanListener<OutStorageSaleListBean>() {
            @Override
            public void onSuccess(OutStorageSaleListBean bean) {
                allData = bean.getBody();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_out_storage_sale_list_no_data));
                }
                addOutStorageSaleListData(context, ActivityUtils.getShowData(allData, 0), null, saleOutStorageList, true);
            }
        });
    }

    private void getMaterielData(Context context, String condition) {
        OutStorageSaleMaterielListRequest.request(context, condition, false, new GsonBeanListener<OutStorageSaleMaterielListBean>() {
            @Override
            public void onSuccess(OutStorageSaleMaterielListBean bean) {
                allMaterielData = bean.getBody();
                if (allMaterielData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_out_storage_sale_list_no_data));
                }
                addOutStorageSaleListData(context, null, ActivityUtils.getShowData(allMaterielData, 0), saleOutStorageList, true);
            }
        });
    }

    public interface OutStorageSaleListener {
        void onNoDoubleClickListener(String condition);

        void onChangeTab(int flag);
    }

}
